/*
 * @Date: 2025-06-18 11:24:28
 * @Auth: 463997479@qq.com
 * @LastEditors: 463997479@qq.com
 * @LastEditTime: 2025-09-02 15:21:34
 * @FilePath: \corpus\src\pages\Home\EditCorpus\index.tsx
 */
/*
 * @Date: 2025-06-13 10:50:10
 * @Auth: 463997479@qq.com
 * @LastEditors: 463997479@qq.com
 * @LastEditTime: 2025-06-18 11:30:30
 * @FilePath: \corpus\src\pages\Home\EditCorpus\index.tsx
 */
import { Form, Input, Modal, Select } from 'antd';
import type React from 'react';
import { useEffect, useMemo } from 'react';
import hooks from '../hooks/useHooks';
const EditCorpus: React.FC = () => {
  const { TextArea } = Input;
  const {
    corpuVisible,
    setCorpuVisible,
    type,
    formCorpus,
    detail,
    addKnowledgeContent,
    editKnowledgeContent,
    setType,
  } = hooks.useContainer();

  const flag = useMemo(() => {
    return Object.keys(detail).length !== 0;
  }, [detail]);
  const title = useMemo(() => {
    return flag
      ? `${type === 'jc' || type === 'zw' ? '修改语料库' : '修改知识库'}`
      : `${type === 'jc' || type === 'zw' ? '新增语料库' : '新增知识库'}`;
  }, [type, flag]);
  const handleSubmit = () => {
    formCorpus.validateFields().then((vals) => {
      const method = flag ? editKnowledgeContent : addKnowledgeContent;
      method.run({
        ...vals,
      });
      formCorpus.resetFields();
    });
  };

  useEffect(() => {
    if (corpuVisible && flag) {
      formCorpus.setFieldsValue({
        ...detail,
      });
    }
  }, [flag, corpuVisible]);

  return (
    <Modal
      width={600}
      title={title}
      open={corpuVisible}
      onCancel={() => {
        setCorpuVisible(false);
        formCorpus.resetFields();
        setType('');
      }}
      onOk={handleSubmit}
      confirmLoading={flag ? editKnowledgeContent.loading : addKnowledgeContent.loading}
    >
      <Form labelCol={{ span: 4 }} form={formCorpus}>
        <Form.Item name="id" hidden />
        <Form.Item
          label={type === 'jc' || type === 'zw' ? '语料库名称' : '知识库名称'}
          name="name"
          rules={[{ required: true }]}
        >
          <Input disabled={flag ? true : false} placeholder="请输入" />
        </Form.Item>
        <Form.Item label={'类型'} name="type" rules={[{ required: true, message: '请选择' }]}>
          <Select
            style={{ width: '100% ' }}
            placeholder="请选择"
            options={[
              {
                label: '个人',
                value: 'gr',
              },
              {
                label: '部门',
                value: 'bm',
              },

              {
                label: '场景',
                value: 'cj',
              },
              {
                label: '公共',
                value: 'gg',
              },
              {
                label: '基础',
                value: 'jc',
              },
              {
                label: '政务',
                value: 'zw',
              },
            ]}
            disabled={flag || type ? true : false}
          />
        </Form.Item>

        <Form.Item label={'描述'} name="knowledgeDescribe" rules={[{ required: true }]}>
          <TextArea autoSize={{ minRows: 4 }} placeholder="请输入" />
        </Form.Item>
      </Form>
    </Modal>
  );
};

export default EditCorpus;
